package com.wei.czz.framework.common.controller;

import com.wei.czz.common.annotation.AuthLogin;
import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.dto.common.fileFlow.FileFlowDto;
import com.wei.czz.common.dto.common.fileFlow.FilePreUploadDto;
import com.wei.czz.common.dto.common.fileFlow.FileUploadDto;
import com.wei.czz.common.jsr303.group.Search;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.vo.common.fileFlow.*;
import com.wei.czz.framework.common.manager.FileFlowManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2025-03-23 12:39:15
 * className: FileFlowController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/common/file-flow")
@AllArgsConstructor
public class FileFlowController {

    private static final Logger log = LoggerFactory.getLogger(FileFlowController.class);

    private final FileFlowManager fileFlowManager;

    /**
     * 文件上传接口
     *
     * @param fileUploadVo 文件上传参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'fileUpload' + #userId + '-' + #fileUploadVo.fileHash", message = "文件上传")
    @PostMapping("/upload")
    @AuthLogin
    public Result<FileUploadDto> fileUpload(@Validated @ModelAttribute FileUploadVo fileUploadVo) {
        log.info("开始文件上传业务。{}", fileUploadVo);

        FileUploadDto fileUploadDto = fileFlowManager.fileUpload(fileUploadVo);

        log.info("文件上传业务结束");
        return Result.success(fileUploadDto);
    }

    /**
     * 切片文件上传接口
     *
     * @param uploadVo 文件上传参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'chunkFileUpload' + #userId + '-' + #uploadVo.chunkFileHash", message = "切片文件上传")
    @PostMapping("/chunk/upload")
    @AuthLogin
    public Result<Object> chunkFileUpload(@Validated @ModelAttribute ChunkFileUploadVo uploadVo) {
        log.info("开始切片文件上传业务。{}", uploadVo);

        fileFlowManager.chunkFileUpload(uploadVo);

        log.info("切片文件上传业务结束");
        return Result.success();
    }

    /**
     * 大文件上传创建接口
     *
     * @param preUploadVo 创建参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'filePreUpload' + #userId", message = "大文件上传创建")
    @PostMapping("/pre-upload")
    @AuthLogin
    public Result<FilePreUploadDto> filePreUpload(@Validated @RequestBody FilePreUploadVo preUploadVo) {
        log.info("开始大文件上传创建业务。{}", preUploadVo);

        FilePreUploadDto filePreUploadDto = fileFlowManager.filePreUpload(preUploadVo);

        log.info("大文件上传创建业务结束");
        return Result.success(filePreUploadDto);
    }

    /**
     * 获取切片文件大小接口
     *
     * @return 结果
     */
    @GetMapping("/chunk/file-size")
    public Result<Long> getChunkFileSize() {
        log.info("开始获取切片文件大小业务");

        Long chunkFileSize = fileFlowManager.getChunkFileSize();

        log.info("获取切片文件大小业务结束");
        return Result.success(chunkFileSize);
    }

    /**
     * 分页获取文件上传记录列表接口
     *
     * @param formVo 查询参数对象
     * @return 结果
     */
    @PostMapping("/page/list")
    @AuthLogin
    public Result<PageDto<FileFlowDto>> getFileFlowPageList(@Validated(Search.class) @RequestBody FileFlowFormVo formVo) {
        log.info("开始分页获取文件上传记录列表业务。{}", formVo);

        PageDto<FileFlowDto> pageDto = fileFlowManager.getFileFlowPageList(formVo);

        log.info("分页获取文件上传记录列表业务结束");
        return Result.success(pageDto);
    }

    /**
     * 构建文件接口
     *
     * @param fileNo 文件编号
     * @return 结果
     */
    @GetMapping("/build")
    @AuthLogin
    public Result<FileUploadDto> buildFile(@RequestParam("fileNo") Long fileNo) {
        log.info("开始构建文件业务。fileNo={}", fileNo);

        FileUploadDto fileUploadDto = fileFlowManager.buildFile(fileNo);

        log.info("构建文件业务结束");
        return Result.success(fileUploadDto);
    }

    /**
     * 大文件合并接口
     *
     * @param mergeVo 合并参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'bigFileMerge' + #userId", message = "大文件合并")
    @PostMapping("/big-file/merge")
    @AuthLogin
    public Result<FileUploadDto> bigFileMerge(@Validated @RequestBody BigFileMergeVo mergeVo) {
        log.info("开始大文件合并业务。{}", mergeVo);

        FileUploadDto fileUploadDto = fileFlowManager.bigFileMerge(mergeVo);

        log.info("大文件合并业务结束");
        return Result.success(fileUploadDto);
    }

    /**
     * 文件压缩接口
     *
     * @param zipFileVo 参数对象
     * @return 结果
     */
    @PostMapping("/zip")
    @AuthLogin
    public Result<Long> zipFile(@Validated @RequestBody ZipFileVo zipFileVo) {
        log.info("开始文件压缩业务。{}", zipFileVo);

        Long fileNo = fileFlowManager.zipFile(zipFileVo);

        log.info("文件压缩业务结束");
        return Result.success(fileNo);
    }
}
